package com.jing.cloud.service.chat.adapter.helper;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jing.cloud.service.chat.adapter.MessageBean;
import com.jing.cloud.service.chat.bean.Message;
import com.jing.cloud.service.chat.bean.UserMessage;
import com.jing.cloud.service.chat.constant.ChatConstant;
import com.jing.cloud.service.chat.dao.MessageDao;
import com.jing.cloud.service.chat.dao.UserMessageDao;
import com.jing.cloud.service.util.db.Compare;
import com.jing.cloud.service.util.db.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by 29017 on 2017/9/12.
 */
@Component
public class MessageHelper {

    //    @Autowired
//    private MessageCursorDao messageCursorDao;
    @Autowired
    private UserMessageDao userMessageDao;
    @Autowired
    private MessageDao messageDao;

    public List<Long> getUserIds(MessageBean messageBean) {
        List<Long> all = Lists.newArrayList();
        if (messageBean.getScene() == ChatConstant.MESSAGE_SCENE_U2U) {
            all.add(messageBean.getSender());
            all.add(messageBean.getTarget());
        } else if (messageBean.getScene() == ChatConstant.MESSAGE_SCENE_GROUP) {
            // TODO: 2017/9/12 处理群 
        }
        if (null == messageBean.getLimitUserList() || messageBean.getLimitUserList().isEmpty()) {
            return all;
        } else {
            List<Long> userIds = Lists.newArrayList();
            for (Long userId : messageBean.getLimitUserList()) {
                if (all.contains(userId)) {
                    userIds.add(userId);
                }
            }
            return userIds;
        }
    }


    public List<UserMessage> list(long userId, long id, int type) {
        Map<String, Object> condition = Maps.newHashMap();
        condition.put("userId", userId);
        Compare c = new Compare();
        c.setGt(id);
        condition.put("id", c);

        Compare c3 = new Compare();
        c3.setNe(ChatConstant.MESSAGE_PUSH_YES);
        condition.put("push" + type, c3);
        Page<UserMessage> page = new Page<>();
        page.setPage(1);
        page.setPageSize(100);
        page = userMessageDao.query(condition, page);
        List<UserMessage> userMessages = page.getList();
        if (userMessages.isEmpty()) {
            return userMessages;
        }
        List<Long> messageIds = Lists.transform(page.getList(), new Function<UserMessage, Long>() {
            @Override
            public Long apply(UserMessage userMessage) {
                return userMessage.getMessageId();
            }
        });
        condition.clear();
        Compare c2 = new Compare();
        c2.setIn(messageIds);
        condition.put("id", c2);
        List<Message> messages = messageDao.query(condition);
        for (UserMessage um : userMessages) {
            for (Message m : messages) {
                if (um.getMessageId().equals(m.getId())) {
                    um.setMessage(m);
                    break;
                }
            }
        }
        return userMessages;
    }

//    public MessageCursor getMessageCursor(long userId,int deviceType){
//        Map<String,Object> condition = Maps.newHashMap();
//        condition.put("userId",userId);
//        condition.put("deviceType",deviceType);
//        List<MessageCursor> list = messageCursorDao.query(condition);
//        if (list!=null&&!list.isEmpty()){
//            return list.get(0);
//        }
//        return null;
//    }

}
